<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title">商城管理系统</div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
            prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            prefix-icon="el-icon-lock"
            @keyup.enter.native="handleLogin"
          >
            <i
              slot="suffix"
              class="el-icon-view"
              @click="showPwd"
            />
          </el-input>
        </el-form-item>

        <!-- <el-form-item prop="captcha">
          <div class="captcha-container">
            <el-input
              ref="captcha"
              v-model="loginForm.captcha"
              placeholder="验证码"
              name="captcha"
              type="text"
              tabindex="3"
              autocomplete="off"
              prefix-icon="el-icon-key"
              style="width: 200px"
            />
            <img
              src="captchaUrl"
              class="captcha-image"
              @click="refreshCaptcha"
            />
          </div>
        </el-form-item> -->

        <el-button
          :loading="loading"
          type="primary"
          style="width:100%; margin-bottom:30px;"
          @click.native.prevent="handleLogin"
        >登录</el-button>

        <div class="tips">
          <span>用户名: admin</span>
          <span>密码: 随便填</span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/auth'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        captcha: '1234'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      passwordType: 'password',
      captchaUrl: '',
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.refreshCaptcha()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    refreshCaptcha() {
      getCaptcha().then(response => {
        const { data } = response.data
        this.captchaUrl = data.image_base64
        console.log(data)
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('auth/login', this.loginForm)
            .then(() => {
              // 登录成功后获取用户信息
              return this.$store.dispatch('auth/getInfo')
            })
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
              this.refreshCaptcha()
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-card {
    width: 520px;
    padding: 20px 35px;
    border-radius: 10px;

    .title {
      font-size: 26px;
      color: #333;
      margin: 0 auto 30px auto;
      text-align: center;
      font-weight: bold;
    }

    .login-form {
      .captcha-container {
        display: flex;
        align-items: center;
        
        .captcha-image {
          margin-left: 10px;
          height: 40px;
          cursor: pointer;
        }
      }

      .tips {
        font-size: 14px;
        color: #999;
        margin-bottom: 10px;
        text-align: center;

        span {
          &:first-of-type {
            margin-right: 16px;
          }
        }
      }
    }
  }
}
</style> 